package com.zw.mes.module.tecard.controller;



import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.zw.mes.base.BaseController;
import com.zw.mes.common.JsonResult;
import com.zw.mes.entity.flow.FlowSheet;
import com.zw.mes.entity.technology.Result;
import com.zw.mes.entity.technology.Tecard;
import com.zw.mes.module.cost.service.DictCommonService;
import com.zw.mes.module.tecard.service.TecardService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.util.List;

import static com.zw.mes.constant.Constant.GROUP_SPECTYPE;

/**
 * @author mayongfeng
 * @version 1.0.0
 * @ClassName TecardController.java
 * @Description TODO
 * @createTime 2021年03月10日 14:20:00
 */
@Controller
@SuppressWarnings("all")
@RequestMapping("/tecard")
public class TecardController extends BaseController {

    @Autowired
    private DictCommonService dictCommonService;

    @Autowired
    private TecardService tecardService;

    /**
     * @Author mayongfeng
     * @Date 14:15 2021/1/29
     * @Description 页面跳转到工艺卡接收
     *
     **/
    @GetMapping
    public String listPage(Model model, Result result){
        model.addAttribute("special", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        return "technology/card/listPage";
    }

    /**
     * @Author mayongfeng
     * @Date 14:15 2021/1/29
     * @Description 页面跳转到外购件计划分配
     *
     **/
    @GetMapping("/jump")
    public String jumpCard(String proCode,String special,String part,String recStatus,Integer id ,Integer prodChange,Model model){
        model.addAttribute("proCodeId", proCode);
        model.addAttribute("special", special);
        model.addAttribute("part",part);
        model.addAttribute("recStatus",recStatus);
        model.addAttribute("id",id);
        model.addAttribute("prodChange",prodChange);
        return "technology/card/cardDetail";
    }
    
    /**
     * @Author mayongfeng
     * @Date 9:05 2021/3/11
     * @Description 查询工艺卡接收列表页
     *
     **/
    @ResponseBody
    @PostMapping("/list")
    public JsonResult cardList(Tecard tecard){
        startPage();
        List<Tecard> tecardList = tecardService.getCardRevice(tecard);
        return jsonResult(tecardList);
    }


    /**
     * @Author mayongfeng
     * @Date 9:05 2021/3/11
     * @Description 查询项目下所有图号的工艺卡
     *
     **/
    @PostMapping("/selectMakeDetail")
    @ResponseBody
    public JsonResult selectMakeDetail(Tecard tecard){
        startPage();
        List<Tecard> tecardList = tecardService.getMakeDetail(tecard);
        return jsonResult(tecardList);
    }


    /** 查询每个图号工艺卡的明细*/
    @GetMapping("/full/{id}")
    public String print(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("card", tecardService.getCardById(id));
        model.addAttribute("details", tecardService.findCardDetailByCardId(id));
        return "technology/card/full";
    }

    /**
     * @Author mayongfeng
     * @Date 15:19 2021/3/12
     * @Description 工艺卡接收方法
     *
     **/
    @PostMapping("/cardReceive")
    @ResponseBody
    public JsonResult cardReceive(String tecards) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm").create();
        Tecard tecard = gson.fromJson(tecards, Tecard.class);
        int i = tecardService.cardReceive(tecard);

        return jsonResult(i);
    }

/***********************************************工艺卡打印，工艺卡查询**************************************************/
    /**
     * @Author mayongfeng
     * @Date 14:15 2021/1/29
     * @Description 页面跳转到工艺卡查询
     *
     **/
    @GetMapping("/print")
    public String printPage(Model model, Result result){
        model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        return "technology/card/listPrint";
    }

    /** 打印工艺卡列表 */
    @ResponseBody
    @PostMapping("query/list")
    public JsonResult cardPrintList(Tecard tecard) {
        startPage();
        List<FlowSheet> list = tecardService.findWithCardQuery(tecard);
        return jsonResult(list);
    }

    /**
     * @Author mayongfeng
     * @Date 13:39 2021/3/17
     * @Description 批量打印
     *
     **/
    @GetMapping("print/p")
    public String print(Integer[] ids, Model model) {
        model.addAttribute("cards", tecardService.findCardListFull(ids));
        return "technology/card/print";
    }

    /** 查询生产的每个图号工艺卡的明细*/
    @GetMapping("/selectProdCard/{id}")
    public String selectProdCard(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("card", tecardService.getProdCardById(id));
        model.addAttribute("details", tecardService.findProdCardDetailByCardId(id));
        return "technology/card/prodFull";
    }


}
